
define(["avalon"], function(avalon){
	var dummyStyle = document.createElement('div').style,
	vendor = (function () {
		var vendors = 't,webkitT,MozT,msT,OT'.split(','),
			t,
			i = 0,
			l = vendors.length;

		for ( ; i < l; i++ ) {
			t = vendors[i] + 'ransform';
			if ( t in dummyStyle ) {
				return vendors[i].substr(0, vendors[i].length - 1);
			}
		}

		return false;
	})(),
	cssVendor = vendor ? '-' + vendor.toLowerCase() + '-' : '',

	// Style properties
	transform = prefixStyle('transform'),
	transitionProperty = prefixStyle('transitionProperty'),
	transitionDuration = prefixStyle('transitionDuration'),
	transformOrigin = prefixStyle('transformOrigin'),
	transitionTimingFunction = prefixStyle('transitionTimingFunction'),
	transitionDelay = prefixStyle('transitionDelay'),

    // Browser capabilities
	isAndroid = (/android/gi).test(navigator.appVersion),
	isIDevice = (/iphone|ipad/gi).test(navigator.appVersion),
	isTouchPad = (/hp-tablet/gi).test(navigator.appVersion),

    has3d = prefixStyle('perspective') in dummyStyle,
    hasTouch = 'ontouchstart' in window && !isTouchPad,
    hasTransform = vendor !== false,
    hasTransitionEnd = prefixStyle('transition') in dummyStyle,

	RESIZE_EV = 'onorientationchange' in window ? 'orientationchange' : 'resize',
	START_EV = hasTouch ? 'touchstart' : 'mousedown',
	MOVE_EV = hasTouch ? 'touchmove' : 'mousemove',
	END_EV = hasTouch ? 'touchend' : 'mouseup',
	CANCEL_EV = hasTouch ? 'touchcancel' : 'mouseup',
	TRNEND_EV = (function () {
		if ( vendor === false ) return false;

		var transitionEnd = {
				''			: 'transitionend',
				'webkit'	: 'webkitTransitionEnd',
				'Moz'		: 'transitionend',
				'O'			: 'otransitionend',
				'ms'		: 'MSTransitionEnd'
			};

		return transitionEnd[vendor];
	})(),
	
	nextFrame = (function() {
		return window.requestAnimationFrame ||
			window.webkitRequestAnimationFrame ||
			window.mozRequestAnimationFrame ||
			window.oRequestAnimationFrame ||
			window.msRequestAnimationFrame ||
			function(callback) { return setTimeout(callback, 1); };
	})(),
	cancelFrame = (function () {
		return window.cancelRequestAnimationFrame ||
			window.webkitCancelAnimationFrame ||
			window.webkitCancelRequestAnimationFrame ||
			window.mozCancelRequestAnimationFrame ||
			window.oCancelRequestAnimationFrame ||
			window.msCancelRequestAnimationFrame ||
			clearTimeout;
	})(),

	// Helpers
	translateZ = has3d ? ' translateZ(0)' : '';
	
	function prefixStyle (style) {
		if ( vendor === '' ) return style;
	
		style = style.charAt(0).toUpperCase() + style.substr(1);
		return vendor + style;
	}
	
	//console.log("uscroll.js")
	var widget = avalon.ui.uscroll = function(element, data, vmodels) {
		var options = data.uscrollOptions; //★★★取得配置项
		
        var getYPos = function (e) {
        	var originEvent;
        	if (e.originalEvent) {
        		origin = e.originalEvent;
        	} else {
        		origin = e;
        	}
        	
        	if (origin.changedTouches) {
        		return origin.changedTouches[0].screenY;
        	} else {
        		return origin.screenY;
        	}
//        	return window.scrollY;
        }
        
        function debuglog(str) {
        	if (vmodel.debug) {
        		var debugConsole = document.getElementById("debug");
        		var uscrollDebug = document.getElementById("uscroll-debug");
        		if (!uscrollDebug) {
        			uscrollDebug = document.createElement("div");
        			uscrollDebug.id = "uscroll-debug";
        			debugConsole.appendChild(uscrollDebug);
        		}
        		
        		uscrollDebug.innerHTML = str;
        	}
        }
        
        function getClientHeight() {
        	return window.document.documentElement.clientHeight;
        }
            
		var vm = {
			$id : data.uscrollId,
			$init : function() {//初始化组件的界面，最好定义此方法，让框架对它进行自动化配置
                
                avalon.scan(element, [vmodel].concat(vmodels))
                if(typeof vmodel.onInit === "function"){
                   vmodel.onInit.call(element, vmodel, options, vmodels);
                }
                
//                avalon(element).css("top", "-1");
//                avalon(element).css("bottom", "0");
//                avalon(element).css("overflow-y", "auto");
//                avalon(element).css("position", "fixed");
                
                window.scrollTo(0,1);
                avalon.bind(element, MOVE_EV, function (e) { e.preventDefault(); }, false);
                
                //console.log(element.parentNode);
                avalon.bind(element, START_EV, vmodel.start);
                avalon.bind(element, END_EV, vmodel.end);
                avalon.bind(element, MOVE_EV, vmodel.move);
                avalon.bind(element,  CANCEL_EV, vmodel.end);
			},
			$remove : function() {//清空构成UI的所有节点，最好定义此方法，让框架对它进行自动化销毁
				
                avalon.unbind(element, START_EV, vmodel.start);
                avalon.unbind(element, END_EV, vmodel.end);
                avalon.unbind(element, MOVE_EV, vmodel.move);
                avalon.bind(element,  CANCEL_EV, vmodel.end);
            },
            $data : {
            	startPos : 0,
            	nowPos : 0,
            	startScrollTop : 0,
            	drag : false
            },
            data : {
            	tipText : "",
            	paddingTop : 0
            },
            start : function (e) {
//            	var p = getClientHeight();
//            	debuglog("window.scrollY:" + window.scrollY + 
//            		"<br>element.offsetTop:" +element.offsetTop + 
//            		"<br>element.parentNode.offsetTop:" +element.parentNode.offsetTop + 
//            		"<br>element.parentNode.scrollHeight:" +element.parentNode.scrollHeight + 
//            		"<br>window.screen.height:" + window.screen.height+
//            		"<br>getClientHeight:" + p
//            		);
            	vmodel.$data.startPos = getYPos(e);
				vmodel.$data.nowPos = vmodel.$data.startPos;
            	vmodel.$data.drag = true;
            	vmodel.$data.startScrollTop = element.scrollTop;
            },
            end : function (e) {
            	vmodel.move(e);
            	vmodel.$data.drag = false;
            },
            move : function(e) {
            	
            	if (!vmodel.$data.drag) {
            		return;
            	}
            	var yPos = getYPos(e);
            	vmodel.$data.nowPos = yPos;
            	
            	var height =  yPos - vmodel.$data.startPos+2;
            	var moveTo = vmodel.$data.startScrollTop - height;
            	if (moveTo <= 0) {
            		moveTo = 1;
            	}
            	
            	debuglog("moveTo:" + moveTo);
            	element.scrollTop = moveTo;
            	
            }
        }
		avalon.mix(vm, options);
        
		var vmodel = avalon.define(vm);
		return vmodel;
	}
		
	widget.defaults = {//默认配置项
		debug: true
    }
    
	return avalon;
});